package com.ee.lock.controller;

import com.ee.lock.dao.LockMapper;
import com.ee.lock.service.LockService;
import com.ee.lock.utills.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author LiBo
 * @date 2022年03月02日 15:41
 */

@RestController
public class LockController {

    @Autowired
    LockService lockService;

    @Autowired
    LockMapper lockMapper;

    @PostMapping("/lock")
    public ResultBody addLock(@RequestParam("telephone") String telephone){
        try {
            return ResultBody.success(lockService.addLock(telephone));
        }catch (Exception e){
            e.printStackTrace();
            return ResultBody.error("添加失败");
        }
    }

    @GetMapping("/lock")
    public ResultBody getLock(){
        try {
            return ResultBody.success(lockService.getAllLock());
        }catch (Exception e){
            e.printStackTrace();
            return ResultBody.error("获取失败");
        }
    }

    @PutMapping("/lock")
    public ResultBody updateStatus(@RequestParam("id") String id,
                                   @RequestParam("status") Integer status){
        try {
            return ResultBody.success(lockService.updateStatus(id, status));
        }catch (Exception e){
            e.printStackTrace();
            return ResultBody.error("修改错误");
        }
    }

    @GetMapping("/lock/{id}")
    public ResultBody getLockById(@PathVariable("id") String id){
        try {
            return ResultBody.success(lockMapper.getLockById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultBody.error("获取失败");
        }
    }
}
